package com.example.databinding.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.recyclerview.widget.RecyclerView;

import com.example.databinding.R;
import com.example.databinding.bean.Idols;
import com.example.databinding.databinding.ItemBinding;

import java.util.List;
import java.util.Objects;

public class IdolRecycleViewAdapter extends RecyclerView.Adapter<IdolRecycleViewAdapter.MyViewHolder> {

    List<Idols> list;

    public IdolRecycleViewAdapter(List<Idols> list) {
        this.list = list;
    }

    @NonNull
    @Override
    //创建的item都从这里取出
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        //利用DataBi
        ItemBinding itemBinding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.item, parent, false);
        return new MyViewHolder(itemBinding);
    }

    //给创建的item设置数据
    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        Idols idol = list.get(position);
        //从创建的holder中取出dataBinding 并设置值
        holder.itemBinding.setIdol(idol);
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    //创建viewHolder
    static class MyViewHolder extends RecyclerView.ViewHolder {

        public ItemBinding itemBinding;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
        }

        public MyViewHolder(ItemBinding itemBinding) {
            //将binding获取的view传给itemView
            super(itemBinding.getRoot());
            this.itemBinding = itemBinding;
        }
    }
}
